package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.ContactUser;
import com.naihe.anderson.sms.entity.NhsmsSession;
import com.naihe.anderson.sms.manager.ContactMgr;

public class HorizontalSmsAdapter extends BaseAdapter {
	
	private Context mContext;
	private List<NhsmsSession> mObjects;
	private ContactMgr contactMgr;
	private Drawable drawable;
	
	public HorizontalSmsAdapter(Context context, List<NhsmsSession> objects) {
		this.mContext = context;
		this.mObjects = objects;
		contactMgr = ((NhSmsApp)context.getApplicationContext()).getContactMgr();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			view = inflater.inflate(R.layout.horizontal_sms_item, null);
		} else {
			view = convertView;
		}
		NhsmsSession entityObj = mObjects.get(position);
		
		ImageView contactImage = (ImageView) view.findViewById(R.id.contactImage);
		TextView smsNum = (TextView) view.findViewById(R.id.sms_num);
		TextView smsTime = (TextView) view.findViewById(R.id.sms_time);
		TextView smsContent = (TextView) view.findViewById(R.id.sms_content);
		RelativeLayout bottom_layout = (RelativeLayout) view.findViewById(R.id.bottom_layout);
		TextView smsName = (TextView) view.findViewById(R.id.sms_name);
		
		if(entityObj == null){
			contactImage.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.new_item_bg));
			bottom_layout.setVisibility(View.GONE);
			smsContent.setVisibility(View.GONE);
		} else {
			 ContactUser contactUser = contactMgr.getContactUserById(contactMgr.getContactIdByPhoneNum(entityObj.getAddress()));
			if(contactUser != null && contactUser.getPhotoId() > 0){
				contactImage.setBackgroundDrawable(new BitmapDrawable(contactMgr.getContactHeadImgByPhotoId(contactUser.getPhotoId())));
			} else {
				contactImage.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.default_head));
			}
			
			if(contactUser != null){
				smsName.setText(contactUser.getName());
			} else {
				smsName.setText(entityObj.getAddress());
			}
			smsContent.setVisibility(View.VISIBLE);
			bottom_layout.setVisibility(View.VISIBLE);
			if(entityObj.getRead() == 0){
				bottom_layout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.new_line_bk));
			} else {
				bottom_layout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.line_bk));
			}
			smsNum.setText(entityObj.getNumsize() + "");
			smsTime.setText(getDataFormat(entityObj.getDate()));
			if(TextUtils.isEmpty(entityObj.getCipher())){
				smsContent.setText(entityObj.getBody());
				smsContent.setCompoundDrawables(null, null, null, null);
			} else {
				smsContent.setText("");
				if(drawable == null){
					drawable = mContext.getResources().getDrawable(R.drawable.lock);
					drawable.setBounds(0, 0, 40, 40);
				}
				smsContent.setCompoundDrawables(drawable, null, null, null);
			}
		}
		
		return view;
	}

	private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            Date nowDate = new Date();
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDay()==nowDate.getDay()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDay()==(nowDate.getDay()-1)){
				formatTime = mContext.getResources().getString(R.string.yesterday) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear()){
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);;
        }
        return formatTime;
    }
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(mObjects == null)
			return 0;
		return mObjects.size();
	}

	@Override
	public NhsmsSession getItem(int position) {
		// TODO Auto-generated method stub
		if(mObjects == null)
			return null;
		return mObjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
}
